package com.wm.blog_web.task;

import com.wm.blog_common.enums.EsOperationEnum;
import com.wm.blog_config.Document.BlogDocument;
import com.wm.blog_web.feign.SearchFeignClient;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ：半卷流年
 * @description : 添加博客成功,往es中添加文档
 * @createTime ：2020/5/23
 */
@Slf4j
public class BlogSaveTask implements Runnable {

    private BlogDocument blogDocument;

    private SearchFeignClient searchFeignClient;

    /**
     * @see com.wm.blog_common.enums.EsOperationEnum
     */
    private Integer esOperation;

    public BlogSaveTask(BlogDocument blogDocument, SearchFeignClient searchFeignClient, Integer esOperation) {
        this.blogDocument = blogDocument;
        this.searchFeignClient = searchFeignClient;
        this.esOperation = esOperation;
    }

    @Override
    public void run() {
        if (esOperation.equals(EsOperationEnum.SAVE.getValue())) {
            log.info("博客保存成功,往es中添加文档:{}", blogDocument.toString());
            searchFeignClient.add(blogDocument);
        } else if(esOperation.equals(EsOperationEnum.EDITOR.getValue())){
            log.info("博客修改成功,修改es中的数据:{}",blogDocument.toString());
            searchFeignClient.updateDocument(blogDocument);
        }else if (esOperation.equals(EsOperationEnum.DELETE.getValue())){
            searchFeignClient.delete(blogDocument.getId());
            log.info("博客删除成功,修改es中的数据:{}",blogDocument.toString());
        }else{
            log.info("es操作类型错误,不进行任何处理.....");
        }
    }
}
